/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.statistics.top;

import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.form.core.TwoDatesForm;
import com.blandware.atleap.common.util.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.Globals;
import org.apache.commons.validator.GenericValidator;

import java.util.Locale;
import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;

/**
 * <p>Contains some common utils for actions which desplay statistics tops.</p>
 * <p><a href="ListTopObjectsActionSupport.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.4 $ $Date: 2006/06/03 11:53:50 $
 */
public abstract class ListTopObjectsActionSupport extends BaseAction {

    /**
     * Processes 'from' and 'till' dates: if they are not parseable, default
     * values are set; also, dates are stored into request.
     *
     * @param request request
     * @param form form
     */
    protected void processDates(HttpServletRequest request, ActionForm form) {
        HttpSession session = request.getSession();
        Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
        if (locale == null) {
            locale = new Locale("en");
        }

        Calendar tillCalendar = Calendar.getInstance();
        tillCalendar.add(Calendar.DAY_OF_MONTH, 1);
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.add(Calendar.YEAR, -1);

        TwoDatesForm twoDatesForm = (TwoDatesForm) form;
        String fromDateStr = twoDatesForm.getDate1();
        String tillDateStr = twoDatesForm.getDate2();

        Date fromDate = tryToParseDate(fromDateStr, fromCalendar.getTime(),
                                       locale);
        Date tillDate = tryToParseDate(tillDateStr, tillCalendar.getTime(),
                                       locale);
        twoDatesForm.setDate1(DateUtil.formatDate(fromDate, locale));
        twoDatesForm.setDate2(DateUtil.formatDate(tillDate, locale));

        request.setAttribute("fromDate", fromDate);
        request.setAttribute("tillDate", tillDate);
    }

    /**
     * Parses date; if it's empty or cannot be parsed, default date is returned.
     *
     * @param dateStr string to parse
     * @param defaultDate default date to return if given string is empty or
     * cannot be parsed
     * @param locale locale in which to parse date
     * @return parsed date or default one if given string cannot be parsed
     */
    protected Date tryToParseDate(String dateStr, Date defaultDate,
                                  Locale locale) {
        Date date = defaultDate;
        if (!GenericValidator.isBlankOrNull(dateStr)) {
            try {
                date = DateUtil.parseDate(dateStr, locale);
            } catch (ParseException e) {
            }
        }
        return date;
    }
}
